//
//  WFPasswordManagerUtilies.m
//  WFTravel
//
//  Created by mc on 2017/11/8.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "LLPasswordManagerUtilies.h"
#import <SAMKeychain/SAMKeychain.h>
#import "LLRSAUtilies.h"

#define sam_service @"www.admin.com"
#define sam_account @"123456789"

NSString *const tokenKey = @"wwwlgcom_tokenKey";

@implementation LLPasswordManagerUtilies

+ (BOOL)saveUUIDString:(NSString *)uuidString
{
    return [SAMKeychain setPassword:uuidString forService:@"com.admin.www" account:@"uuidAccount"];
}

+ (NSString *)getUUIDString
{
    return [SAMKeychain passwordForService:@"com.admin.www" account:@"uuidAccount"];
}

+ (BOOL)savePassword:(NSString *)password account:(NSString *)account
{
    return [SAMKeychain setPassword:password forService:sam_service account:account];
}

+ (BOOL)savePasswordData:(NSData *)password account:(NSString *)account
{
    return [SAMKeychain setPasswordData:password forService:sam_service account:account];
}

+ (nullable NSArray<NSDictionary<NSString *, id> *> *)allAccounts
{
    return [SAMKeychain allAccounts];
}

+ (nullable NSString *)passwordForAccount:(NSString *)account
{
    return [SAMKeychain passwordForService:sam_service account:account];
}

+ (nullable NSData *)passwordDataAccount:(NSString *)account
{
    return [SAMKeychain passwordDataForService:sam_service account:account];
}

+ (BOOL)deletePasswordAccount:(NSString *)account
{
    return [SAMKeychain deletePasswordForService:sam_service account:account];
}


/**
 保存登录用户信息
 */
+ (void)saveLoginUserInfoWithModel:(TTLoginModel *)loginModel
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"user.data"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:filePath isDirectory:nil]) {
        [manager createFileAtPath:filePath contents:nil attributes:nil];
        
    }
    
    [NSKeyedArchiver archiveRootObject:loginModel toFile:filePath];
    
}

/**
 获取登录用户信息
 */
+ (TTLoginModel *)getLoginUserInfo
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"user.data"];
    
     TTLoginModel *loginModel = (TTLoginModel *)[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    return loginModel;
}


/**
 删除 登录用户信息
 */
+ (void)deleteLoginUserInfo
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [path stringByAppendingPathComponent:@"user.data"];
    
    NSError *error = nil;
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:filePath]) {
        [fm removeItemAtPath:filePath error:&error];
    }
    if (error) {
        NSLog(@"删除用户信息失败：%@", error);
    }
    
}


+ (void)saveDatasWithKey:(NSString *)key object:(id)obj
{
    if (obj == nil) {
        return;
    }
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *type = [NSString stringWithFormat:@"%@.data", key];
    NSString *filePath = [path stringByAppendingPathComponent:type];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    if (![manager fileExistsAtPath:filePath isDirectory:nil]) {
        [manager createFileAtPath:filePath contents:nil attributes:nil];
        
    }
    
    [NSKeyedArchiver archiveRootObject:obj toFile:filePath];
    
}


+ (id)getDatasWithKey:(NSString *)key
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *type = [NSString stringWithFormat:@"%@.data", key];
    NSString *filePath = [path stringByAppendingPathComponent:type];
    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

+ (void)deleteDataWithKey:(NSString *)key
{
    if (key.length == 0) {
        return;
    }
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *type = [NSString stringWithFormat:@"%@.data", key];
    NSString *filePath = [path stringByAppendingPathComponent:type];
    
    NSError *error = nil;
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:filePath]) {
        [fm removeItemAtPath:filePath error:&error];
    }
    if (error) {
        NSLog(@"删除用户信息失败：%@", error);
    }
}

///保存当前的版本号
+ (void)saveCurrentVersion
{
    [USER_DEFAULT setObject:kCurrentVersion forKey:@"lastVersion"];
}

///获取上次保存的版本号
+ (NSString *)getLastVersion
{
    return [USER_DEFAULT objectForKey:@"lastVersion"];
}


@end
